//
//  StatusOriginalView.swift
//  微博三天
//
//  Created by jessica on 15/11/15.
//  Copyright © 2015年 jessica. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

//StatusOriginalView原创微薄里面包含的子控件

class StatusOriginalView: UIView {
    
    //原创微博底部约束 ,用来记录底部约束 后续根据是否有配图来动态修改
    var bottomConstraints : Constraint?
    
    
    //数据模型
    var sta : Status?{
        
        didSet{ //这个作用就是把之前固定的数值进行修改为网络里提供的数值
            
            iconImage.sd_setImageWithURL(sta?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = sta?.user?.name
            //            memberIconView.image = sta?user?.mbrankImage
            
            memberIconView.image = sta?.user?.mbrankImage
            verifiedIconView.image = sta?.user?.verified_type_Image
            timeLabel.text = "11:11" //sta?.created_at
            sourceLabel.text = "隔壁老王"//sta?.source
            contentLabel.text = sta?.text
            
            //先移除约束,不操作
            self.bottomConstraints?.uninstall()
            //给配图设置数据源
            if let picurls = sta?.imageURLs where picurls.count > 0{
                //有配图
                pictureView.imageURLs = sta?.imageURLs
                //显示
                pictureView.hidden = false
                //将约束条件转换成 约束类型
                
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
            }else{
                //没有配图 , 隐藏配图视图
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
            }
        }
    }
    
    
    //默认构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置ui
    private func setupUI(){
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(verifiedIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        iconImage.snp_makeConstraints { (make) -> Void in

                make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(snp_left).offset(StatusCellMargin)
                make.height.equalTo(StatusCellIconWitdth)
                make.width.equalTo(StatusCellIconWitdth)
            }
            nameLabel.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(iconImage.snp_top)
                make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            }
            
            memberIconView.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(nameLabel.snp_top)
                make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            }
            
            verifiedIconView.snp_makeConstraints { (make) -> Void in
                make.centerX.equalTo(iconImage.snp_right)
                make.centerY.equalTo(iconImage.snp_bottom)
            }
            
            timeLabel.snp_makeConstraints { (make) -> Void in
                make.bottom.equalTo(iconImage.snp_bottom)
                make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            }
            
            
            sourceLabel.snp_makeConstraints { (make) -> Void in
                make.bottom.equalTo(timeLabel.snp_bottom)
                make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            }
            
            contentLabel.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(iconImage)
            }
            //pictureView添加调试
            pictureView.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(contentLabel.snp_left)
                //PictureView里面有didSet重设,这是固定数据
                //            make.width.equalTo(100)
                //            make.height.equalTo(100)
                
            }
            
            //原创微博的底部相对于微博正文的底部对齐
            self.snp_makeConstraints { (make) -> Void in
                
                //            make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
                //约束条件转成 约束类型
                self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                
                
            }
            
        }
        
        //懒加载控件
        private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        private lazy var nameLabel: UILabel = UILabel(title: "我就是杜蕾他哥", size: 14, color: UIColor.darkGrayColor())
        ///用认证类型户ren'zh
        private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
        //用户等级
        private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
        //发布时间
        private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 10, color: UIColor.orangeColor())
        ///微博来源
        
        private lazy var sourceLabel: UILabel = UILabel(title: "来自: 一道光", size: 10, color: UIColor.lightGrayColor())
        ///微博正文
        private lazy var contentLabel: UILabel = UILabel(title: "呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵额呵呵呵呵呵呵呵呵呵额呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵额呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵额呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵额呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵额呵呵呵呵呵呵呵呵呵呵呵呵呵呵", size: 14, color: UIColor.lightGrayColor(),margin: StatusCellMargin)
        
        //添加pictureView
        private lazy var pictureView : StatusPictureView = StatusPictureView()
        
        
        //懒加载控件
        private lazy var sepView = UIView()
        
        
        
}

